package com.gate.ws.spring.rocketmq.consumer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.gate.ws.spring.cache.LocalChannelCache;
import com.gate.ws.spring.utils.JsonUtils;
import com.nami.basic.bo.mq.DownMsgLogout;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import static com.nami.basic.constants.DownTopicConst.LOGOUT_TOPIC;


@Slf4j
@Component
@RocketMQMessageListener(topic = LOGOUT_TOPIC, consumerGroup = "${rocketmq.consumer.logoutConsumerGroup}", selectorExpression = "${nami.mqTags}", consumeMode = ConsumeMode.CONCURRENTLY)
public class DownLogOutConsumer implements RocketMQListener<String> {


    @Override
    public void onMessage(String msg) {
        try {
            TimeInterval timer = DateUtil.timer();
            DownMsgLogout logout = JsonUtils.parseObject(msg, DownMsgLogout.class);
            Channel channel = LocalChannelCache.get(logout.getIdentityNo());
            channel.writeAndFlush(logout.getPacket());
            // 删除L1Cache
            LocalChannelCache.removeAll(logout.getIdentityNo());
            log.info("[logout] userId:{},use:{}ms", logout.getIdentityNo(), timer.interval());
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("[down-login] error:", var3.getMessage());
        }
    }


}
